﻿using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using PosClient.View;
using FreeSql.Internal;
using System.Threading;
using PosClient.Service;
using Splat;
using System.Security.Permissions;

namespace PosClient
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
        static void Main()
        {
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out bool ret);
            if (ret)
            {
                //设置应用程序处理异常方式：ThreadException处理
                //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                //全局捕获Task中未观察到的异常


                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                AppDomain currentDomain = AppDomain.CurrentDomain;
                ApplicationContext context = new ApplicationContext(new LoginView());
                Application.Run(context);
            }
        }

        private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            string str = DataUtil.GetExceptionMsg(e.Exception, e.ToString());
            try
            {
                Logger.Default.Error(str, e);
            }
            catch (Exception err)
            {
                Logger.Default.Error(err.Message, e);
            }
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            string str = DataUtil.GetExceptionMsg(e.Exception, e.ToString());
            try
            {
                Logger.Default.Error(str, e);
            }
            catch (Exception err)
            {
                Logger.Default.Error(err.Message, e);

            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string str = DataUtil.GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
            try
            {
                Logger.Default.Error(str, e);
            }
            catch (Exception err)
            {
                Logger.Default.Error(err.Message, e);
            }
        }
    }
}
